{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "cf950aed",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "Placebo Tests for the Second Difference-in-Differences Model<br><table style=\"text-align:center\"><tr><td colspan=\"7\" style=\"border-bottom: 1px solid black\"></td></tr>\n",
       "<tr><td style=\"text-align:left\"></td><td colspan=\"6\"><em>Dependent variable: Employment</em></td></tr><tr><td style=\"text-align:left\"></td><tr><td></td><td colspan=\"1\">2014 & 2015</td><td colspan=\"1\">2014 & 2016</td><td colspan=\"1\">2014 & 2017</td><td colspan=\"1\">2015 & 2016</td><td colspan=\"1\">2015 & 2017</td><td colspan=\"1\">2016 & 2017</td></tr><tr><td style=\"text-align:left\"></td><td>(1)</td><td>(2)</td><td>(3)</td><td>(4)</td><td>(5)</td><td>(6)</td></tr>\n",
       "<tr><td colspan=\"7\" style=\"border-bottom: 1px solid black\"></td></tr>\n",
       "\n",
       "<tr><td style=\"text-align:left\">Intercept</td><td>11104.67<sup>***</sup></td><td>11104.67<sup>***</sup></td><td>11104.67<sup>***</sup></td><td>10624.22<sup>***</sup></td><td>10624.22<sup>***</sup></td><td>10043.08<sup>***</sup></td></tr>\n",
       "<tr><td style=\"text-align:left\"></td><td>(2412.05)</td><td>(2412.05)</td><td>(2412.05)</td><td>(2313.10)</td><td>(2313.10)</td><td>(2193.67)</td></tr>\n",
       "<tr><td style=\"text-align:left\">Treat</td><td>6061.26<sup>*</sup></td><td>6061.26<sup>*</sup></td><td>6061.26<sup>*</sup></td><td>5335.60<sup>*</sup></td><td>5335.60<sup>*</sup></td><td>5055.64<sup>*</sup></td></tr>\n",
       "<tr><td style=\"text-align:left\"></td><td>(3107.71)</td><td>(3107.71)</td><td>(3107.71)</td><td>(2972.86)</td><td>(2972.86)</td><td>(2837.26)</td></tr>\n",
       "<tr><td style=\"text-align:left\">FalsePost</td><td>-480.45<sup></sup></td><td>-1061.59<sup></sup></td><td>-1228.66<sup></sup></td><td>-581.14<sup></sup></td><td>-748.20<sup></sup></td><td>-167.07<sup></sup></td></tr>\n",
       "<tr><td style=\"text-align:left\"></td><td>(3341.92)</td><td>(3260.40)</td><td>(3240.34)</td><td>(3187.89)</td><td>(3167.37)</td><td>(3081.23)</td></tr>\n",
       "<tr><td style=\"text-align:left\">Treat×FalsePost</td><td>-725.66<sup></sup></td><td>-1005.63<sup></sup></td><td>-978.09<sup></sup></td><td>-279.96<sup></sup></td><td>-252.43<sup></sup></td><td>27.54<sup></sup></td></tr>\n",
       "<tr><td style=\"text-align:left\"></td><td>(4300.67)</td><td>(4208.08)</td><td>(4200.35)</td><td>(4109.50)</td><td>(4101.58)</td><td>(4004.39)</td></tr>\n",
       "\n",
       "<td colspan=\"7\" style=\"border-bottom: 1px solid black\"></td></tr>\n",
       "<tr><td style=\"text-align: left\">Observations</td><td>1020</td><td>1020</td><td>1020</td><td>1020</td><td>1020</td><td>1020</td></tr><tr><td style=\"text-align: left\">R<sup>2</sup></td><td>0.01</td><td>0.01</td><td>0.01</td><td>0.01</td><td>0.01</td><td>0.01</td></tr><tr><td style=\"text-align: left\">Adjusted R<sup>2</sup></td><td>0.00</td><td>0.00</td><td>0.00</td><td>0.00</td><td>0.00</td><td>0.00</td></tr><tr><td style=\"text-align: left\">Residual Std. Error</td><td>34338.07 (df=1016)</td><td>33598.79 (df=1016)</td><td>33537.07 (df=1016)</td><td>32811.71 (df=1016)</td><td>32748.51 (df=1016)</td><td>31972.49 (df=1016)</td></tr><tr><td style=\"text-align: left\">F Statistic</td><td>2.40<sup>*</sup> (df=3; 1016)</td><td>2.55<sup>*</sup> (df=3; 1016)</td><td>2.63<sup>**</sup> (df=3; 1016)</td><td>2.18<sup>*</sup> (df=3; 1016)</td><td>2.23<sup>*</sup> (df=3; 1016)</td><td>2.14<sup>*</sup> (df=3; 1016)</td></tr>\n",
       "<tr><td colspan=\"7\" style=\"border-bottom: 1px solid black\"></td></tr><tr><td style=\"text-align: left\">Note:</td><td colspan=\"6\" style=\"text-align: right\"><sup>*</sup>p&lt;0.1; <sup>**</sup>p&lt;0.05; <sup>***</sup>p&lt;0.01</td></tr></table>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#### Objective:\n",
    "\n",
    "    # This code implements a set of placebo DID regressions for \"Employment\", across several worksheet-year \n",
    "    # pairs from the Excel file, using “Treat” based on the Não Simples indicator. It checks for spurious treatment \n",
    "    # effects in years before the actual policy.\n",
    "\n",
    "\n",
    "#### Workflow:\n",
    "\n",
    "    # Iterates over worksheets/periods, filtering only the pre and placebo-post years.\n",
    "    # Constructs dummies:\n",
    "    # Treat: 1 if not in the \"Simples\" regime (based on the Não Simples column).\n",
    "    # FalsePost: 1 for the fake post-period, 0 otherwise.\n",
    "    # Estimates DID (with robust errors) for each panel.\n",
    "    # Gathers results in a professional summary table (Stargazer for Python).\n",
    "    # Displays results in the notebook or saves as HTML.\n",
    "    \n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import statsmodels.formula.api as smf\n",
    "from stargazer.stargazer import Stargazer\n",
    "from IPython.display import display, HTML\n",
    "\n",
    "# 1) File path and worksheet-to-fake-post-treatment-year mapping\n",
    "file_path = 'Data Placebo Test2.xlsx'\n",
    "sheets_and_post_years = {\n",
    "    '2014_2015': 2015,\n",
    "    '2014_2016': 2016,\n",
    "    '2014_2017': 2017,\n",
    "    '2015_2016': 2016,\n",
    "    '2015_2017': 2017,\n",
    "    '2016_2017': 2017\n",
    "}\n",
    "\n",
    "# 2) Lists to store regression results and labels\n",
    "placebo_models_2 = []\n",
    "placebo_labels_2 = []\n",
    "\n",
    "# 3) Loop through each worksheet: Placebo DID regressions on 'Employment'\n",
    "for sheet_name, placebo_post_year in sheets_and_post_years.items():\n",
    "    # Read spreadsheet\n",
    "    df = pd.read_excel(file_path, sheet_name=sheet_name)\n",
    "    df.rename(columns={'Yit_Emprego': 'Employment'}, inplace=True)\n",
    "    \n",
    "    # Filter only for the two relevant years (pre- and placebo-post years)\n",
    "    placebo_pre_year = int(sheet_name.split('_')[0])\n",
    "    df = df[df['Ano'].isin([placebo_pre_year, placebo_post_year])].copy()\n",
    "    \n",
    "    # Dummies: 'Treat' is constructed from the 'Não Simples' column; 'FalsePost' for fake post-treatment period\n",
    "    df['Treat'] = df['Não Simples'].astype(int)\n",
    "    df['FalsePost'] = np.where(df['Ano'] == placebo_post_year, 1, 0)\n",
    "    \n",
    "    # Estimate placebo DID model with robust standard errors (HC1)\n",
    "    model = smf.ols('Employment ~ Treat * FalsePost', data=df).fit(cov_type='HC1')\n",
    "    \n",
    "    placebo_models_2.append(model)\n",
    "    placebo_labels_2.append(f'{placebo_pre_year} & {placebo_post_year}')\n",
    "\n",
    "# 4) Generate Stargazer-style summary table\n",
    "stargazer2 = Stargazer(placebo_models_2)\n",
    "stargazer2.title(\"Placebo Tests for the Second Difference-in-Differences Model\")\n",
    "stargazer2.custom_columns(placebo_labels_2, [1] * len(placebo_models_2))\n",
    "stargazer2.significant_digits(2)\n",
    "\n",
    "stargazer2.rename_covariates({\n",
    "    'Treat': 'Treat',\n",
    "    'FalsePost': 'FalsePost',\n",
    "    'Treat:FalsePost': 'Treat×FalsePost'\n",
    "})\n",
    "\n",
    "stargazer2.covariate_order([\n",
    "    'Intercept',    # or 'const'\n",
    "    'Treat',\n",
    "    'FalsePost',\n",
    "    'Treat:FalsePost'\n",
    "])\n",
    "\n",
    "# 5) Display the summary in the notebook\n",
    "display(HTML(stargazer2.render_html()))\n",
    "\n",
    "# 6) Optionally save as HTML for sharing\n",
    "with open('placebo2_did_emprego.html', 'w', encoding='utf-8') as f:\n",
    "    f.write(stargazer2.render_html())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "02965332",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
